<template>
  <a-card :bordered="false">
    <div class="card-title">{{ $route.meta.title }}</div>
    <div class="table-operator">
      <!-- 操作板块 -->
      <div class="row-item-tab clearfix">
        <a-button class="fl-l" type="primary" icon="delete" @click="handleDeletes">批量删除链接</a-button>
      </div>
    </div>
    <s-table
      ref="table"
      rowKey="id"
      :loading="isLoading"
      :columns="columns"
      :data="loadData"
      :pageSize="15"
      :rowSelection="rowSelection"
    >
      <!-- 操作 -->
      <span class="actions" slot="action" slot-scope="text, item">
        <a style="color: red" v-action:delete @click="handleDelete(item.id)">删除</a>
      </span>
    </s-table>
  </a-card>
</template>

<script>
import * as Api from '@/api/ad'
import { STable } from '@/components'
import adActionType from '@/common/enum/adActionType'
export default {
  name: 'Index',
  components: {
    STable
  },
  data() {
    return {
      // 当前表单元素
      searchForm: this.$form.createForm(this),
      // 查询参数
      queryParam: {},
      // 正在加载
      isLoading: false,
      adActionType,
      // 表头
      columns: [
        {
          title: 'id',
          dataIndex: 'id'
        },
        {
          title: 'book_id',
          dataIndex: 'book_id'
        },
        {
          title: 'wx_user_id',
          dataIndex: 'wx_user_id'
        },
        {
          title: 'book_type',
          dataIndex: 'book_type'
        },
        {
          title: 'config_id',
          dataIndex: 'config_id'
        },
        {
          title: '创建时间',
          dataIndex: 'create_time'
        },
        {
          title: '操作',
          dataIndex: 'action',
          width: '180px',
          scopedSlots: { customRender: 'action' }
        }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: (param) => {
        return Api.getLinkList({ ...param, ...this.queryParam }).then((response) => response.data.list)
      },
      // 已选择的元素
      selectedRowKeys: []
    }
  },
  created() {},
  computed: {
    rowSelection() {
      return {
        selectedRowKeys: this.selectedRowKeys,
        onChange: this.onSelectChange
      }
    }
  },
  methods: {
    /**
     * 删除记录
     */
    handleDelete(id) {
      const app = this
      const modal = this.$confirm({
        title: '您确定要删除该链接吗?',
        content: '删除后不可恢复',
        onOk() {
          return Api.deleteLink({ id })
            .then((result) => {
              app.$message.success(result.message, 1.5)
              app.handleRefresh()
            })
            .finally((result) => modal.destroy())
        }
      })
    },

    handleDeletes() {
      if (this.selectedRowKeys.length === 0) {
        this.$message.warning(`请勾选链接后删除!`, 1)
        return
      }
      const app = this
      const modal = this.$confirm({
        title: '您确定要删除已勾选链接吗?',
        content: '删除后不可恢复',
        onOk() {
          return Promise.all(
            app.selectedRowKeys.map(async (id) => {
              console.log('id: ', id)
              return await Api.deleteLink({ id })
            })
          )
            .then((result) => {
              console.log('result: ', result)
              app.$message.success('批量删除成功', 1.5)
              app.handleRefresh()
            })

            .finally((result) => modal.destroy())
        }
      })
    },

    /**
     * 刷新列表
     * @param Boolean bool 强制刷新到第一页
     */
    handleRefresh(bool = false) {
      this.$refs.table.refresh(bool)
    },
    // 选中项发生变化时的回调
    onSelectChange(selectedRowKeys, newSelectedItems) {
      this.selectedRowKeys = selectedRowKeys
    }
  }
}
</script>

<style lang="less" scoped>
.ant-card-body {
  padding: 22px 29px 25px;
}
</style>
